package com.ybl.store.backend.controller.user;

import com.ybl.store.backend.dao.entity.user.ClerkDO;
import com.ybl.store.backend.dto.req.user.clerk.ClerkSaveReqDTO;
import com.ybl.store.backend.dto.req.user.clerk.ClerkUpdateReqDTO;
import com.ybl.store.backend.dto.resp.user.clerk.ClerkNameRespDTO;
import com.ybl.store.backend.dto.resp.user.clerk.ClerkPageRespDTO;
import com.ybl.store.backend.service.user.ClerkService;
import com.ybl.store.common.context.UserContext;
import com.ybl.store.common.convention.result.Result;
import com.ybl.store.common.convention.result.Results;
import com.ybl.store.common.entity.ListRespDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 店员控制层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/backend/clerk")
public class ClerkController {
    
    private final ClerkService clerkService;

    /**
     * 新增店员
     */
    @PostMapping
    public Result<Void> saveClerk(@RequestBody ClerkSaveReqDTO requestParam) {
        clerkService.saveClerk(requestParam);
        return Results.success();
    }

    /**
     * 删除店员
     */
    @DeleteMapping
    public Result<Void> deleteClerkById(@RequestParam Long id) {
        clerkService.deleteClerkById(id);
        return Results.success();
    }

    /**
     * 修改店员
     */
    @PutMapping
    public Result<Void> updateClerk(@RequestBody ClerkUpdateReqDTO requestParam) {
        clerkService.updateClerk(requestParam);
        return Results.success();
    }

    /**
     * 禁用店员
     */
    @PutMapping("/ban")
    public Result<Void> banClerk(@RequestParam Long id, @RequestParam Integer banFlag) {
        clerkService.banClerk(id, banFlag);
        return Results.success();
    }

    /**
     * 分页查询店员
     */
    @GetMapping("/page")
    public Result<ListRespDTO<ClerkPageRespDTO>> pageClerk(
            @RequestParam("pn") Integer pn,
            @RequestParam("ps") Integer ps,
            @RequestParam(required = false) Long storeId,
            @RequestParam(required = false) String name)
    {
        return Results.success(clerkService.pageClerk(pn, ps, storeId, name));
    }

    /**
     * 查询店员名列表
     */
    @GetMapping("/list/name")
    public Result<ListRespDTO<ClerkNameRespDTO>> listClerkName() {
        return Results.success(clerkService.listClerkName());
    }

    @GetMapping("/self/get")
    public Result<ClerkDO> getClerk() {
        return Results.success(clerkService.getById(UserContext.get().getId()));
    }

    @PutMapping("/self/update")
    public Result<Void> updateClerk(@RequestBody ClerkDO clerkDO) {
        clerkDO.setId(UserContext.get().getId());
        clerkService.updateById(clerkDO);
        return Results.success();
    }
}
